﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using Exhibition.Common.Helper;
using Exhibition.Dao.ViewModel;

namespace Exhibition.Service
{
   public  class FileUploadService
    {
        public HttpContext Context { get; private set; }
        public string PostData { get; set; }
        public string StrValue { get; set; }
        public FileUploadService(HttpContext context)
        {
            Context = context;
            if (Context.Request["strValue"] != null)
                StrValue = Context.Request["strValue"];
            if (Context.Request["HfId"] != null)
                PostData = Context.Request["HfId"];
        }
        FileUploadHelper uf = new FileUploadHelper();
        /// <summary>
        /// 网站logo
        /// </summary>
        /// <returns></returns>
        public string UploadLogo()
        {
            HttpPostedFile oFile = Context.Request.Files["Filedata"];

            StringBuilder sb = new StringBuilder();
            uf.MyFile = oFile;
            if (uf.Upload())
            {
                SiteModel stMdl = CenterService.siteService.GetOneById(Convert.ToInt32(PostData));
                if (stMdl == null) {
                    stMdl = new SiteModel();
                }
                stMdl.CreateTime = DateTime.Now.ToString("yyyy-MM-dd");
                stMdl.WebLogoPath = uf.FilePath;
                stMdl.WebLogo = oFile.FileName;
                int id= CenterService.siteService.Save(stMdl);

                if (id > 0)
                {
                    sb.AppendFormat("{0}|{1}",  uf.FilePath, id);
                }
            }

            return sb.ToString();

        }
        /// <summary>
        /// 网站logo
        /// </summary>
        /// <returns></returns>
        public string Uploadflowpic()
        {
            HttpPostedFile oFile = Context.Request.Files["Filedata"];

            StringBuilder sb = new StringBuilder();
            uf.MyFile = oFile;
            if (uf.Upload())
            {
                SiteModel stMdl = CenterService.siteService.GetOneById(Convert.ToInt32(PostData));
                if (stMdl == null)
                {
                    stMdl = new SiteModel();
                }
               // stMdl.CreateTime = DateTime.Now.ToString("yyyy-MM-dd");
                stMdl.FlowPic = uf.FilePath;
              
                int id = CenterService.siteService.Save(stMdl);

                if (id > 0)
                {
                    sb.AppendFormat("<img src='{0}' width='160' height='120'/>|{1}", uf.FilePath, id);
                }
            }

            return sb.ToString();

        }
       /// <summary>
       /// 上传楼层图片
       /// </summary>
       /// <returns></returns>
        public string Uploadfloor()
        {
            HttpPostedFile oFile = Context.Request.Files["Filedata"];

            StringBuilder sb = new StringBuilder();
            uf.MyFile = oFile;
            if (uf.Upload())
            {
                FloorModel fmld = CenterService.floorService.GetOneById(Convert.ToInt32(PostData));
                if (fmld == null)
                {
                    fmld = new FloorModel();
                }
                // stMdl.CreateTime = DateTime.Now.ToString("yyyy-MM-dd");
                fmld.FloorPath = uf.FilePath; 
                int id = CenterService.floorService.Save(fmld); 
                if (id > 0)
                {
                    sb.AppendFormat("<img src='{0}' width='160' height='120'/>|{1}", uf.FilePath, id);
                }
            }

            return sb.ToString();

        }

        public string UploadUserlogo() {

            HttpPostedFile oFile = Context.Request.Files["Filedata"];

            StringBuilder sb = new StringBuilder();
            uf.MyFile = oFile;
            if (uf.Upload())
            {
                UserCompModel fmld = CenterService.userCompService.GetOneById(Convert.ToInt32(PostData));
                if (fmld == null)
                {
                    fmld = new UserCompModel();
                }
                // stMdl.CreateTime = DateTime.Now.ToString("yyyy-MM-dd");
                fmld.CompLogo = uf.FilePath;
                int id = CenterService.userCompService.Save(fmld);
                if (id > 0)
                {
                    sb.AppendFormat("<img src='{0}' width='160' height='120'/>|{1}", uf.FilePath, id);
                }
            }

            return sb.ToString();
        
        }

        public string UploadBadLogo()
        {

            HttpPostedFile oFile = Context.Request.Files["Filedata"];

            StringBuilder sb = new StringBuilder();
            uf.MyFile = oFile;
            if (uf.Upload())
            {
                BadgesModel fmld = CenterService.badgesService.GetOneById(Convert.ToInt32(PostData));
                if (fmld == null)
                {
                    fmld = new BadgesModel();
                }
                var user = MembershipService.GetLoginUser();
                if (user != null) {
                    fmld.SiteId = user.SiteID;
                    fmld.CreateUserId = user.Id;
                    fmld.CreateUserName = user.UserName;
                }
                fmld.CreateTime = DateTime.Now;
                fmld.UserLogo = uf.FilePath;
                int id = CenterService.badgesService.Save(fmld);
                if (id > 0)
                {
                    sb.AppendFormat("{0}|{1}", uf.FilePath, id);
                }
            }

            return sb.ToString();

        }

        public string UploadSitelogo() {

            HttpPostedFile oFile = Context.Request.Files["Filedata"];

            StringBuilder sb = new StringBuilder();
            uf.MyFile = oFile;
            if (uf.Upload())
            {
                FrindLinkModel fmld = CenterService.frindLinkService.GetOneById(Convert.ToInt32(PostData));
                if (fmld == null)
                {
                    fmld = new FrindLinkModel();
                }
                var user = MembershipService.GetLoginUser();
                if (user != null)
                { 
                    fmld.CreateUserId = user.Id;
                 
                }
             
                fmld.SitePic = uf.FilePath;
                int id = CenterService.frindLinkService.Save(fmld);
                if (id > 0)
                {
                    sb.AppendFormat("{0}|{1}", uf.FilePath, id);
                }
            }

            return sb.ToString();
        }


    }
}
